package com.example.tse.rbhttpservice.service.moments;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsGetActivityList;
import com.example.tse.rbhttpservice.service.moments.model.MomentsGetActivityListRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsGetActivityListResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsArticleDetailRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsArticleDetailResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsFavoriteRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsFavoriteResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsGetList;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsGetListRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsGetListResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsGetRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsGetResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsLikeRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsLikeResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsPraiseRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsPraiseResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsRemoveRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsRemoveResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsSubmitRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsSubmitResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsUploadPicRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsMomentsUploadPicResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;


/**
 * 
 * @author wliu
 *    edi  tse
 */
public class MomentsServiceImpl implements MomentsService {
	private Context context;
	private static Gson gson = new Gson();
	
	
	public MomentsServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 获取活动列表，用于发表帖子时选择活动，仅需要ID＋名称
	 * @param request
	 * @param listener
	 * @return MomentsGetActivityListResponse
	 */		
	 
	    public void getActivityList(MomentsGetActivityListRequest request, final RemoteServiceListenerT<MomentsGetActivityListResponse> listener) {

        final HttpParams params = new HttpParams();
        


        final String url = "moments/getActivityList";


        
        /**
        moments
        getActivityList
        GetActivityList
        获取活动列表，用于发表帖子时选择活动，仅需要ID＋名称
        GET
        Moments
        MomentsGetActivityList
        moments/getActivityList
        array
        */
	
           final Type type = new TypeToken<List<MomentsGetActivityList>>() {
        }.getType();



            new HttpService().getList(context, url, params, MomentsGetActivityListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<MomentsGetActivityList> items = gson.fromJson(rudderResponse.getBody(), type);
                 MomentsGetActivityListResponse responce = new MomentsGetActivityListResponse();
                 responce.setGetActivityList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new MomentsGetActivityListResponse());
                 } else {
                     List<MomentsGetActivityList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsGetActivityListResponse responce = new MomentsGetActivityListResponse();
                     responce.setGetActivityList(items);
                     listener.okData(rudderResponse == null ? new MomentsGetActivityListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new MomentsGetActivityListResponse(), fromWhere);
                 } else {
                     List<MomentsGetActivityList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsGetActivityListResponse responce = new MomentsGetActivityListResponse();
                     responce.setGetActivityList(items);
                     listener.okData(rudderResponse == null ? new MomentsGetActivityListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }
         });
    }            
        
        

   
	
	
	/**
	 * 帖子可以点赞，点赞不可取消
	 * @param request
	 * @param listener
	 * @return MomentsMomentsPraiseResponse
	 */		
	 
	    public void praise(MomentsMomentsPraiseRequest request, final RemoteServiceListenerT<MomentsMomentsPraiseResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("articleId",request.getArticleId());

        final String url = "moments/moments/praise";


            new HttpService().post(context, url, params, MomentsMomentsPraiseResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MomentsMomentsPraiseResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MomentsMomentsPraiseResponse() : (MomentsMomentsPraiseResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MomentsMomentsPraiseResponse() : (MomentsMomentsPraiseResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 收藏或取消收藏文章，若传递状态则根据值确定收藏或取消
	 * @param request
	 * @param listener
	 * @return MomentsMomentsFavoriteResponse
	 */		
	 
	    public void favorite(MomentsMomentsFavoriteRequest request, final RemoteServiceListenerT<MomentsMomentsFavoriteResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("articleId",request.getArticleId());
            params.put("status",request.getStatus());

        final String url = "moments/moments/favorite";


            new HttpService().post(context, url, params, MomentsMomentsFavoriteResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MomentsMomentsFavoriteResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MomentsMomentsFavoriteResponse() : (MomentsMomentsFavoriteResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MomentsMomentsFavoriteResponse() : (MomentsMomentsFavoriteResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 发表帖子，帖子内容140字内，可以只上传图片
	 * @param request
	 * @param listener
	 * @return MomentsMomentsSubmitResponse
	 */		
	 
	    public void submit(MomentsMomentsSubmitRequest request, final RemoteServiceListenerT<MomentsMomentsSubmitResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("content",request.getContent());
            params.put("activityId",request.getActivityId());
            params.put("mainPic",request.getMainPic());
            params.put("tags",request.getTags());

        final String url = "moments/moments/submit";


            new HttpService().post(context, url, params, MomentsMomentsSubmitResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MomentsMomentsSubmitResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MomentsMomentsSubmitResponse() : (MomentsMomentsSubmitResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MomentsMomentsSubmitResponse() : (MomentsMomentsSubmitResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 文章是否点赞，是否收藏 分享内容
	 * @param request
	 * @param listener
	 * @return MomentsMomentsArticleDetailResponse
	 */		
	 
	    public void articleDetail(MomentsMomentsArticleDetailRequest request, final RemoteServiceListenerT<MomentsMomentsArticleDetailResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("articleId",request.getArticleId());

        final String url = "moments/moments/articleDetail";


            new HttpService().post(context, url, params, MomentsMomentsArticleDetailResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MomentsMomentsArticleDetailResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MomentsMomentsArticleDetailResponse() : (MomentsMomentsArticleDetailResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MomentsMomentsArticleDetailResponse() : (MomentsMomentsArticleDetailResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 帖子可以点赞，点赞不可取消
	 * @param request
	 * @param listener
	 * @return MomentsMomentsLikeResponse
	 */		
	 
	    public void like(MomentsMomentsLikeRequest request, final RemoteServiceListenerT<MomentsMomentsLikeResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("momentsId",request.getMomentsId());

        final String url = "moments/moments/like";


            new HttpService().post(context, url, params, MomentsMomentsLikeResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MomentsMomentsLikeResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MomentsMomentsLikeResponse() : (MomentsMomentsLikeResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MomentsMomentsLikeResponse() : (MomentsMomentsLikeResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 用于首页、我的主页、活动的帖子，我的主页需要传递memberId（为空为当前用户），活动需要传递activityId
	 * @param request
	 * @param listener
	 * @return MomentsMomentsGetListResponse
	 */		
	 
	    public void getList(MomentsMomentsGetListRequest request, final RemoteServiceListenerT<MomentsMomentsGetListResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("activityId",request.getActivityId());
            params.put("sort",request.getSort());
            params.put("from",request.getFrom());
            params.put("memberId",request.getMemberId());

        final String url = "moments/moments/getList";


        
        /**
        moments
        getList
        GetList
        用于首页、我的主页、活动的帖子，我的主页需要传递memberId（为空为当前用户），活动需要传递activityId
        GET
        MomentsMoments
        MomentsMomentsGetList
        moments/moments/getList
        array
        */
	
           final Type type = new TypeToken<List<MomentsMomentsGetList>>() {
        }.getType();



            new HttpService().getList(context, url, params, MomentsMomentsGetListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<MomentsMomentsGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                 MomentsMomentsGetListResponse responce = new MomentsMomentsGetListResponse();
                 responce.setGetList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new MomentsMomentsGetListResponse());
                 } else {
                     List<MomentsMomentsGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsMomentsGetListResponse responce = new MomentsMomentsGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new MomentsMomentsGetListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new MomentsMomentsGetListResponse(), fromWhere);
                 } else {
                     List<MomentsMomentsGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsMomentsGetListResponse responce = new MomentsMomentsGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new MomentsMomentsGetListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }
         });
    }            
        
        

   
	
	
	/**
	 * 获取朋友圈帖子详情，分享（标题［有活动显示活动标题、没有显示固定一句话］＋描述［有帖子内容显示帖子内容、没有显示固定一句话］＋图片＋链接），固定一句话后台维护，每次获取数据返回
	 * @param request
	 * @param listener
	 * @return MomentsMomentsGetResponse
	 */		
	 
	    public void get(MomentsMomentsGetRequest request, final RemoteServiceListenerT<MomentsMomentsGetResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("momentId",request.getMomentId());

        final String url = "moments/moments/get";


            new HttpService().get(context, url, params, MomentsMomentsGetResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MomentsMomentsGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MomentsMomentsGetResponse() : (MomentsMomentsGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MomentsMomentsGetResponse() : (MomentsMomentsGetResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 可以删除用户自己的帖子，同时删除对应的评论
	 * @param request
	 * @param listener
	 * @return MomentsMomentsRemoveResponse
	 */		
	 
	    public void remove(MomentsMomentsRemoveRequest request, final RemoteServiceListenerT<MomentsMomentsRemoveResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("commentId",request.getCommentId());

        final String url = "moments/moments/remove";


            new HttpService().post(context, url, params, MomentsMomentsRemoveResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MomentsMomentsRemoveResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MomentsMomentsRemoveResponse() : (MomentsMomentsRemoveResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MomentsMomentsRemoveResponse() : (MomentsMomentsRemoveResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 朋友圈上传接口
	 * @param request
	 * @param listener
	 * @return MomentsMomentsUploadPicResponse
	 */		
	 
	    public void uploadPic(MomentsMomentsUploadPicRequest request, final RemoteServiceListenerT<MomentsMomentsUploadPicResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("file",request.getFile());

        final String url = "moments/moments/uploadPic";


            new HttpService().post(context, url, params, MomentsMomentsUploadPicResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MomentsMomentsUploadPicResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MomentsMomentsUploadPicResponse() : (MomentsMomentsUploadPicResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MomentsMomentsUploadPicResponse() : (MomentsMomentsUploadPicResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
}